/**
 * <b>项目名称：</b>网络应用框架<br/>
 * <b>包    名：</b>com.hhwy.framework.util<br/>
 * <b>文 件 名：</b>JsonUtils.java<br/>
 * <b>版本信息：</b><br/>
 * <b>日    期：</b>2014年7月28日-下午4:44:51<br/>
 * <b>Copyright (c)</b> 2014恒华伟业科技股份有限公司-版权所有<br/>
 * 
 */
package com.hhwy.framework.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hhwy.framework.persistent.entity.DProperty;
import com.hhwy.framework.persistent.entity.DomainCacheManager;
import com.hhwy.framework.persistent.entity.DomainInfo;

/**
 * <b>类 名 称：</b>JsonUtils<br/>
 * <b>类 描 述：</b><br/>
 * <b>创 建 人：</b>Administrator<br/>
 * <b>修 改 人：</b>Administrator<br/>
 * <b>修改时间：</b>2014年7月28日 下午4:44:51<br/>
 * <b>修改备注：</b><br/>
 * 
 * @version 1.0.0<br/>
 */
public class JsonUtils {

	public static final Logger log = LoggerFactory.getLogger(JsonUtils.class);

	private static ObjectMapper mapper = new ObjectMapper();
	

	/**
	 * toJson(把对象转换成json)<br/>
	 * 
	 * @param value
	 * @return String
	 * @exception
	 * @since 1.0.0
	 */
	public static String toJson(Object value) {
		try {
			return mapper.writeValueAsString(value);
		} catch (Exception e) {
			log.error("转换json失败", e);
			throw new RuntimeException("json转换异常", e);
		}
	}

	/**
	 * toObject(把json转换成功对象)<br/>
	 * 
	 * @param json
	 * @param clazz
	 * @return T
	 * @exception
	 * @since 1.0.0
	 */
	public static <T> T toObject(String json, Class<T> clazz) {
		try {
			return mapper.readValue(json, clazz);
		} catch (Exception e) {
			log.error("json转换对象失败", e);
			throw new RuntimeException("json转换对象异常", e);
		}
	}
	/**
	 * covertValue(把原对象转换成需要的对象)<br/>
	 * 
	 * @param value
	 * @param toValueType
	 * @return T
	 * @exception
	 * @since 1.0.0
	 */
	public static <T> T covertValue(Object value, Class<T> toValueType) {
		return mapper.convertValue(value, toValueType);
	}

	/**
	 * toObjectArrayListFromJson(把json转换成list)<br/>
	 * 
	 * @param json
	 * @param valueTypeRef
	 * @return List<?>
	 * @exception
	 * @since 1.0.0
	 */
	public static List<?> toList(String json,
			TypeReference<?> valueTypeRef) {
		try {
			return mapper.readValue(json, valueTypeRef);
		} catch (Exception e) {
			log.error("json转换list失败", e);
			throw new RuntimeException("json转换list异常", e);
		}
	}

	public static String toJsonByDateFormat(Object value, String pattern) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			mapper.setDateFormat(new SimpleDateFormat(pattern));
			return mapper.writeValueAsString(value);
		} catch (Exception e) {
			log.error("转换json失败", e);
			throw new RuntimeException("json转换异常", e);
		}
	}

	public static List<?> toListByDateFormat(String json,
			TypeReference<List<?>> valueTypeRef, String pattern) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			mapper.setDateFormat(new SimpleDateFormat(pattern));
			return mapper.readValue(json, valueTypeRef);
		} catch (Exception e) {
			log.error("json转换对象失败", e);
			throw new RuntimeException("json转换对象异常", e);
		}
	}

	public static <T> T toObjectByDateFormat(String json, Class<T> clazz,
			String pattern) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			mapper.setDateFormat(new SimpleDateFormat(pattern));
			return mapper.readValue(json, clazz);
		} catch (Exception e) {
			log.error("json转换对象失败", e);
			throw new RuntimeException("json转换对象异常", e);
		}
	}

	/**
	 * 
	 * convert2View(转换属性为显示的格式，如日期从long转为yyyy-MM-dd)<br/>
	 * 
	 * @param json
	 * @param clazz
	 *            void
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @exception
	 * @since 1.0.0
	 */
//	public static String convert2View(JSONObject json, Class clazz)
//			throws InstantiationException, IllegalAccessException {
//		DomainInfo domainInfo = DomainCacheManager.getDomainInfo(clazz);
//		Iterator<String> it = domainInfo.propertyMap.keySet().iterator();
//		while (it.hasNext()) {
//			String key = it.next();
//			DProperty property = domainInfo.propertyMap.get(key);
//			if (property.getDateFormat() != null
//					&& property.getDateFormat().length() > 0) {
//
//				long value = json.getLong(key);
//				json.remove(key);
//				json.put(key,
//						StringUtils.format(value, property.getDateFormat()));
//
//			}
//		}
//		return JSON.toJSONString(json, SerializerFeature.WriteDateUseDateFormat);
//	}
}
